package tautils;
import java.io.*; 
import java.util.*; 

public class RunningAutomatonInputThroughput { 
  private String init_st;
  private String end_st;
  private String cur_st;
  private PrintWriter logfile;
  private Vector t;
  private Vector act;
  private int cur_index,count;
  private int numStates = 7;
  private long xreal;

  public RunningAutomatonInputThroughput() {
    init_st = "l0";
    end_st = "l6";
    cur_st = null;
    t = new Vector();
    act = new Vector();
    xreal = 0;
    cur_index = 0;
    count = 0; 
  }

  public void makeTrans(long time, String action, PrintWriter logfile) {
    if ( count < numStates - 1 ) {
      act.add(action);
      t.add(new Long(time));
      count++;
    } else {      act.set(cur_index,action);
      t.set(cur_index,new Long(time));
      cur_index++;
      if ( cur_index == numStates - 1 ) {
        cur_index = 0;
      }
    }

    if ( count >= numStates - 1 ) {
      int myplace = 0;
      myplace = cur_index;
      for (int i=0; i < numStates-1; i++ ) {
        int whichone = (myplace+i) % (numStates-1);
        int res = intMakeTrans(((Long)(t.elementAt(whichone))).longValue(),(String)act.elementAt(whichone),logfile);
        if ( res == 1 ) {
          logfile.println("VIOLATION DETECTED. This is the trace of the violation:");
          for ( int j=0; j<numStates-1; j++ ) {
            int place = (myplace+j) % (numStates-1);
            logfile.println("  "+act.elementAt(place)+" at T = "+t.elementAt(place));
          }
        }
      }
    }
  }

  public int intMakeTrans(long time, String action, PrintWriter logfile) {
    boolean done = false; 
    long x = time - xreal; 
    if ( cur_st == null ) { 
      cur_st = "l0"; 
    } 
 
    if (  ( done == false ) & cur_st.equals("l0" ) & (  action.equals("req")  )) {
      done = true; 
      cur_st = "l1"; 
      xreal = time; 
    }
 
    if (  ( done == false ) & cur_st.equals("l1" ) & (  action.equals("req")  )) {
      done = true; 
      cur_st = "l2"; 
    }
 
    if (  ( done == false ) & cur_st.equals("l2" ) & (  action.equals("req")  )) {
      done = true; 
      cur_st = "l3"; 
    }
 
    if (  ( done == false ) & cur_st.equals("l3" ) & (  action.equals("req")  )) {
      done = true; 
      cur_st = "l4"; 
    }
 
    if (  ( done == false ) & cur_st.equals("l4" ) & (  action.equals("req")  )) {
      done = true; 
      cur_st = "l5"; 
    }
 
    if (  ( done == false ) & cur_st.equals("l5" ) & (  action.equals("req")  ) & ( x < 30000 ) ) {
      done = true; 
      cur_st = "l6"; 
    }
 
    if ( cur_st.equals("l6") ) { 
      // logfile.println(" *** VIOLATION DETECTED AT T = "+time+ " ***");
      cur_st = null; 
      return 1;
   } 
 
    if ( done == false ) { 
      logfile.println("No transition found, resetting at T = "+time);
      cur_st = null; 
      xreal = 0; 
      return -1;
  } 
 
    if ( done == true ) { 
      logfile.println("Request received at T =  " + time	 );
      return 0;
   } 
 
  return 0;
  }
}
